// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'
import  { ToastPlugin,AlertPlugin,LoadingPlugin,ConfirmPlugin } from 'vux'
import common from '../wechat/static/common/common'
import API from '../wechat/static/constant/urlconstant'
require("./components/common/common.css");
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(LoadingPlugin)
Vue.use(ConfirmPlugin)
Vue.use(VueResource);
Vue.use(require('vue-wechat-title'))
FastClick.attach(document.body)
Vue.config.productionTip = false
console.log(API)
function intercept() {
  router.beforeEach((to, from, next) => {
    if (!common.getCookie("id")) {
      let webUrl = window.location.href;
      if (webUrl.indexOf("code=") > 0) {
        let code = webUrl.split("code=")[1].split("&")[0];
        let url = API.ACCOUNT.SAVE,
          param = {
            code: code
          };
        Vue.http.post(url, param, { emulateJSON: true }).then(
          res => {
            let data = res.data;
            if (data.status == 200) {
              let id = data.data.bussData.id;
              common.setCookie("id", id);
              next();
            } else {
              console.log(data);
              console.log(data.msg);
            }
          },
          res => {
            console.log(res);
          }
        );
        return;
      }
      let test =API.DOMAIN.DOMAIN;
      let online = window.location.href.includes("code")
          ? window.location.href.split("?")[0]
          : window.location.href,
        REDIRECT_URI = window.location.host == "localhost:8080" ? test : 'https://yoe.icebartech.com/wechat',
        APPID = "wx1080880d6578ab37",
        SCOPE = 'snsapi_userinfo',
        STATE = '';
        // next()&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
          location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${REDIRECT_URI}&&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
    } else {
      next();
    }
  });
}


Vue.http.interceptors.push(function(request, next) {
  // request.credentials = false;
  //拦截器
  next(res => {
    //401 登陆过期跳转
    if (res.data.status == 401) {
      common.setCookie("sessionId", "");
      router.replace('/login')
    }
  });
});

 intercept()

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount('#app-box')



